package com.th.supcom.pds.sortingdrug.client.widget;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.th.supcom.pds.common.client.commom.PdsAuditPrescServiceFactory;
import com.th.supcom.pds.common.enums.AvailableEnum;
import com.th.supcom.pds.common.utils.ClientUtils;
import com.th.supcom.pds.config.client.common.PhamConfigServiceFactory;
import com.th.supcom.pds.config.client.model.PhamWindowConfigModel;
import com.th.supcom.pds.config.client.service.PhamWindowConfigRpcAsync;
import com.th.supcom.pds.sortingdrug.client.model.PdsSupplyProModel;
import com.th.supcom.pds.sortingdrug.client.service.SortingDrugRpcAsync;

/**
 * SortingDrugMainPanel主面板.
 * 
 * 药品信息.
 */
public class SortingDrugMainPanel extends LayoutContainer
{
    // 窗口最大个数
    private static final int SIZE = 8;
    private static final SortingDrugRpcAsync s_SORT_DRUG_ASYNC = PdsAuditPrescServiceFactory.getSortingDrugService ();
    private static final PhamWindowConfigRpcAsync S_PHAM_WINDOW_CONFIG_RPC_ASYNC = PhamConfigServiceFactory.getPhamWindowConfigService ();
    private static final String m_deptCode = ClientUtils.getOrgModel ().getOrgCode ();
    private List <SortingDrugDetailPanel> m_windows = new ArrayList <SortingDrugDetailPanel> ();

    public SortingDrugMainPanel ()
    {
        TableLayout layout = new TableLayout (SIZE);
        layout.setCellSpacing (10);
        setLayout (layout);
        createWindows ();
    }

    /**
     * 创建窗口面板
     */
    public void createWindows ()
    {
        if (null == m_deptCode)
        {
            MessageBox.alert ("提示", "没有选择药房", null);
            return;
        }

        PhamWindowConfigModel phamWindowConfig = new PhamWindowConfigModel ();
        phamWindowConfig.setDeptCode (m_deptCode);
        phamWindowConfig.setOpenFlag (Integer.parseInt (AvailableEnum.YES.getEnumCode ()));
        S_PHAM_WINDOW_CONFIG_RPC_ASYNC.findPhamWindowConfigByExample (phamWindowConfig,
                                                                      new AsyncCallback <List <PhamWindowConfigModel>> ()
                                                                      {

                                                                          @Override
                                                                          public void onFailure (Throwable caught)
                                                                          {
                                                                              MessageBox.alert ("提示", "查询窗口失败", null);
                                                                          }

                                                                          @Override
                                                                          public void onSuccess (List <PhamWindowConfigModel> result)
                                                                          {

                                                                              for (PhamWindowConfigModel model : result)
                                                                              {
                                                                                  SortingDrugDetailPanel window = new SortingDrugDetailPanel (
                                                                                                                                              model);
                                                                                  window.setMainPanel (SortingDrugMainPanel.this);
                                                                                  m_windows.add (window);
                                                                                  SortingDrugMainPanel.this.add (window);
                                                                              }
                                                                              SortingDrugMainPanel.this.layout (true);
                                                                              initData ();
                                                                          }
                                                                      });

    }

    /**
     * 初始化数据
     */
    public void initData ()
    {
        s_SORT_DRUG_ASYNC.findPrescDetailInfoByDept (m_deptCode,
                                                     new AsyncCallback <Map <Long, List <PdsSupplyProModel>>> ()
                                                     {

                                                         @Override
                                                         public void onSuccess (Map <Long, List <PdsSupplyProModel>> result)
                                                         {
                                                             initWindowData (result);
                                                         }

                                                         @Override
                                                         public void onFailure (Throwable caught)
                                                         {
                                                             MessageBox.alert ("提示", "查询处方信息失败", null);
                                                         }
                                                     });
    }

    /**
     * 初始化每个窗口的数据
     * 
     * @param map
     */
    private void initWindowData (Map <Long, List <PdsSupplyProModel>> map)
    {
        for (SortingDrugDetailPanel panel : m_windows)
        {
            List <PdsSupplyProModel> models = map.get (panel.getWindowId ());
            panel.initData (models);
        }
    }
}
